/**
 * Created by zqk on 2018/5/16.
 */
$(document).ready(function () {
    var form = new xhForm($('#xh_form'));
    var iTaskId=addressUri.getParam('iTaskId');
    var iProjectId = addressUri.getParam('iProjectId');
    form.parse();

    //滚动进度条 start
    var tag = false,ox = 0,left = 0,bgleft = 0;
    $('.progress_btn').mousedown(function(e) {
        ox = e.pageX - left;
        tag = true;
    });
    $(document).mouseup(function() {
        tag = false;
    });
    $('.progress').mousemove(function(e) {//鼠标移动
        if (tag) {
            left = e.pageX - ox;
            if (left <= 0) {
                left = 0;
            }else if (left > 120) {
                left = 120;
            }
            $('.progress_btn').css('left', left);
            $('.progress_bar').width(left);
            $('.text').html(parseInt((left/120)*100) + '%');
            $('#iProcess').val(parseInt((left/120)*100));
        }
    });
    $('.progress_bg').click(function(e) {//鼠标点击
        if (!tag) {
            bgleft = $('.progress_bg').offset().left;
            left = e.pageX - bgleft;
            if (left <= 0) {
                left = 0;
            }else if (left > 120) {
                left = 120;
            }
            $('.progress_btn').css('left', left);
            $('.progress_bar').animate({width:left},120);
            $('.text').html(parseInt((left/120)*100) + '%');
            $('#iProcess').val(parseInt((left/120)*100));
        }
    });
    //滚动进度条 end

    //开始结束时间
    $(".tFromAt").datetimepicker({
        format: 'yyyy-mm-dd  hh:00:00',
        forceParse: 0,
        startDate: '2010-01-01',
        minView: 1,
        language: 'zh-CN',
        autoclose: true
    }).on('changeDate', function (ev) {
        var startdate = new Date(ev.date);
        var start = Date.parse(startdate) / 1000;
        var end = $('.tEndAt').val();
        if (end) {
            var enddate = new Date(end);
            var endtime = Date.parse(enddate) / 1000;
            if (endtime <= start) {
                window.top.xhTop.showError('错误', '开始时间不能大于完成时间');
            }
        }
    });
    $(".tEndAt").datetimepicker({
        format: 'yyyy-mm-dd  hh:00:00',
        forceParse: 0,
        startDate: '2010-01-01',
        minView: 1,
        language: 'zh-CN',
        autoclose: true
    }).on('changeDate', function (ev) {
        var enddate = new Date(ev.date);
        var end = Date.parse(enddate) / 1000;
        var start = $('.tFromAt').val();
        if (start) {
            var startdate = new Date(start);
            var starttime = Date.parse(startdate) / 1000;
            if (end <= starttime) {
                window.top.xhTop.showError('错误', '开始时间不能大于完成时间');
            }
        }
    });




    var appPackDom = $('#mIds');
    if(members.length > 0){
        var appArr = [];
        $.each(members, function(id, value){
            appArr.push({id: value['iUserId'], text: value['sStaffName']});
        });
        appPackDom.select2({
            placeholder: '选择项目成员',
            data: appArr
        });
    }
    appPackDom.select2({
        placeholder: '选择参与人',
        data: members
    });

    $('#bPublic').switchbutton({
        onChange:function(checked){
            if(checked){
                $('#bPublic').switchbutton('setValue',1);
            }else{
                $('#bPublic').switchbutton('setValue',0);
            }

        }
    });
    if(iTaskId!=null){ //edit
        $.ajax({
            url:moduleUrl+'task/getTaskInfo',
            data:{iTaskId:iTaskId},
            type:'POST',
            dataType:'json',
            success:function (data) {
                form.fill(data);
                $('#father').combotree('setValue', data.iParentId);
                $("#mIds").val(data.memberIds);
                $("#mIds").select2();
                if(data.bPublic == 1){
                    $('#bPublic').switchbutton('check');
                }else{
                    $('#bPublic').switchbutton('uncheck');
                }
                form.bindRequired($('#bt_modal_confirm',parent.document));
                var left = (data.iProcess/100)*120;
                $('.progress_btn').css('left', left);
                $('.progress_bar').width(left);
                $('.text').html(parseInt((left/120)*100) + '%');
                $('#iProcess').val(parseInt((left/120)*100));
            },
            error:function (data) {
                console.log(data);
            }
        });


        $("#father").combotree({
            onBeforeSelect : function(node) {
                var nowNode = $('#father').combotree("tree").tree('find', iTaskId);
                if(node['id']==iTaskId){  //如果是自己
                    return false;
                }
                var flag=false;
                $.each($("#father").combotree("tree").tree('getChildren', nowNode.target),function (index,obj) {
                    if(obj['id']==node['id']){
                        flag=true;
                        return false;
                    }
                });
                if(flag){
                    return false;
                }
            }
        });
    }else { //create
        var iParentId=addressUri.getParam('iParentId');
        if(iParentId!=null){
            $('#father').combotree('setValue', iParentId);
        }
        form.bindRequired($('#bt_modal_confirm',parent.document));
    }

    $('#bt_modal_confirm',parent.document).click(function () {
        xhExtra.setBtnLoading($('#bt_modal_confirm',parent.document));
        $('#bt_modal_confirm',parent.document).addClass('disabled');
        var ifTaskStats = $('#ifTaskStats').val();

        $.ajax({
            url:moduleUrl+'task/saveTask?iProjectId='+iProjectId,
            data:$('#xh_form').serialize(),
            type:'POST',
            success:function (data) {
                window.top.xhTop.showSuccess(
                  data.msg
                );
                if(!ifTaskStats){
                    window.top.xhTop.getActiveFrame().taskList.treegrid('reload');
                    window.top.xhTop.getActiveFrame().projectList.treegrid('reload');
                }else{
                    window.top.xhTop.getActiveFrame().obj.taskList.treegrid('reload');
                }
                window.top.xhTop.closeModal();
            },
            error:function (data) {
                xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                $('#bt_modal_confirm',parent.document).removeClass('disabled');
                window.top.xhTop.showError(
                    data.responseJSON.error,
                    data.responseJSON.msg
                );
            }
        });
    });
    // modalBind.bindClick();

});